package com.oyjd.ybx.fw.action;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.ServletRequestUtils;

/**
 * 分页查询信息
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class Page<T> {
	// ---------------------------------------------参数
	/**
	 * 需要查询的页数
	 */
	private int page;
	/**
	 * 页面大小
	 */
	private int pageSize;
	/**
	 * 过滤条件
	 */
	private Map<String, String[]> paras = new LinkedHashMap<String, String[]>();

	// ---------------------------------------------结果
	/**
	 * 总记录数
	 */
	private long count;
	/**
	 * 分页的记录结果集
	 */
	private List<T> result;

	/**
	 * 构造函数
	 * 
	 */
	public Page() {
		super();
	}

	/**
	 * 构造函数
	 * 
	 * @param req
	 *            请求
	 */
	@SuppressWarnings("rawtypes")
	public Page(HttpServletRequest req) {
		super();
		page = ServletRequestUtils.getIntParameter(req, "page", 1);
		pageSize = ServletRequestUtils.getIntParameter(req, "pageSize", 10);
		//
		Map map = req.getParameterMap();
		for (Iterator item = map.keySet().iterator(); item.hasNext();) {
			String key = (String) item.next();
			if (key.startsWith("q_") || key.startsWith("Q_")) {
				String[] value = (String[]) map.get(key);
				if (!"".equals(value[0])) {
					paras.put(key.substring(2), value);
				}
			}
		}
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Map<String, String[]> getParas() {
		return paras;
	}

	public void setParas(Map<String, String[]> paras) {
		this.paras = paras;
	}

	public long getCount() {
		return count;
	}

	public void setCount(long count) {
		this.count = count;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	/**
	 * 获取当前页的起始位置（从零开始）
	 * 
	 * @return
	 */
	public int getFirst() {
		return (page - 1) * pageSize;
	}

}
